﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../demo.css">
    <script type="text/javascript" src="../jquery.min.js"></script>
    <script type="text/javascript" src="../jquery.cookie.js"></script>
    <script type="text/javascript" src="../jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="../extend.js"></script>
    <script>
        var dataCache = null;
        var appId = GetQueryString("moduleid");
        window.onresize = calSize;
        var rowEditing = -1;
        var fieldEditing = -1;
        var columnsList = [];
        var editing = false;
        var cusId = GetQueryString("customerid");
        var titles = getTitle();
        var customer = dataFetcher("../../api/customer/get?id=" + cusId);
        var employees = getEmployee();
        var employeesInSP = getEmployeeBySalesPoint();
        document.onkeydown = keyDown;
        $(document).ready(function () {
            initBase();
            listDetail();
            calSize();
            getColumnVisable();
        });
        function getColumnVisable() {
            var allColumn = $('#customerHistoryList').datagrid('getColumnFields');
            for (var j = 0; j < allColumn.length; j++) {
                var col = $('#customerHistoryList').datagrid('getColumnOption', allColumn[j]);
                if (!(col.hidden) && (col.editor != undefined)) {
                    columnsList.push(allColumn[j]);
                }
            }
        }
        function initBase() {
            $('#Code').textbox('setValue', customer.Code);
            $('#Name').textbox('setValue', customer.Name);
            $('#Source').textbox('setValue', valToName(customerSourceData, customer.Source));
        }
        function listDetail() {
            $('#customerHistoryList').datagrid('loading');
            $.get("../../api/customerHistory/listbycustomer?customer=" + cusId, function (data) {
                if (data.Result == 0) {
                    $('#customerHistoryList').datagrid({ data: JSON.parse(data.Data) }).datagrid('clientPaging');
                }
            }, "json")
                .error(function () {
                    $.messager.alert("错误", "系统内部错误", "error");
                })
                .complete(function () {
                    $('#customerHistoryList').datagrid('loaded');
                });
        }
        window.onresize = calSize;
        function calSize() {
            var height = window.parent.document.getElementById("mainFrame").clientHeight;
            var width = window.parent.document.getElementById("mainFrame").clientWidth;
            window.parent.document.getElementById(appId).height = height - 35;
            $("#customerHistoryList").datagrid('resize', { height: height - 140, width: width - 20 });
            $("#toolbar").panel({ width: width - 20 });
            $("#editZone").panel({ width: width - 20 });
        }
        function addItem() {
            var date = new Date().toLocaleDateString();
            if (rowEditing > 0) { $('#customerHistoryList').datagrid("endEdit", rowEditing); }
            dataCache = { AutoId: 0, Flag: 0, Date: date, State: 0, CustomerId: cusId };
            $('#customerHistoryList').datagrid('appendRow', dataCache);
        }
        function rowSelected(index, row) {
            if (rowEditing != index) {
                $('#customerHistoryList').datagrid('validateRow', rowEditing);
                $('#customerHistoryList').datagrid("endEdit", rowEditing)
                rowEditing = index;
                dataCache = row;
            }
            if (row.State == 1 || row.State == 0) {
                if (!editing) {
                    $('#customerHistoryList').datagrid("enableCellEditing");
                    editing = true;
                }
            } else {
                if (editing) {
                    $('#customerHistoryList').datagrid("cancelEdit", index);
                    $('#customerHistoryList').datagrid("disableCellEditing");
                    editing = false;
                }
            }
            updateBtnState(dataCache.State);
        }
        function salesFormater(value, row, index) {
            return valToName(employees, value);
        }
        function submitChange() {
            showMask();
            var valid = $('#customerHistoryList').datagrid('validateRow', rowEditing);
            if (!valid) {
                $.messager.alert("错误", "错误的数据格式", "error");
                return;
            }
            $('#customerHistoryList').datagrid('endEdit', rowEditing);
            dataCache = $('#customerHistoryList').datagrid('getSelected');
            $.post("../../api/customerHistory/set", dataCache, function (response) {
                if (response.Result == 0) {
                    dataCache = JSON.parse(response.Data);
                    $('#customerHistoryList').datagrid('updateRow', { index: rowEditing, row: dataCache });
                    $('#customerHistoryList').datagrid('refreshRow', rowEditing);
                    updateBtnState(dataCache.State);
                    $.messager.alert("信息", "操作成功", "info");
                } else {
                    $.messager.alert("错误", response.Message, "error");
                }
            })
                .error(function () {
                    $.messager.alert("错误", "系统内部错误", "error");
                })
                .complete(function () {
                    hideMask();
                });
        }
        function deleteRecord() {
            var selected = $('#customerHistoryList').datagrid('getSelected');
            if (!selected) {
                $.messager.alert("错误", "请先选中要删除的对象", "error");
                return;
            } else {
                dataCache = selected;
            }
            $.messager.confirm('删除提示', '确定删除该记录?', function (r) {
                if (r) {

                    if (dataCache.AutoId && dataCache.AutoId > 0) {
                        showMask();
                        $.post("../../api/customerHistory/remove", dataCache, function (response) {
                            if (response.Result == 0) {
                                $.messager.alert("信息", "操作成功", "info");
                                $('#customerHistoryList').datagrid('cancelEdit', rowEditing).datagrid('deleteRow', rowEditing);
                                rowEditing = -1;
                            } else {
                                $.messager.alert("错误", response.Message, "error");
                            }
                        })
                            .error(function () {
                                $.messager.alert("错误", "系统内部错误", "error");
                            })
                            .complete(function () {
                                hideMask();
                            });
                    } else {
                        $('#customerHistoryList').datagrid('cancelEdit', rowEditing).datagrid('deleteRow', rowEditing);
                        rowEditing = -1;
                    }

                }
            });
        }

        function setCellEditable() {
            $('#customerHistoryList').datagrid('validateRow', rowEditing);
            $('#customerHistoryList').datagrid('endEdit', rowEditing);
            $('#customerHistoryList').datagrid('selectRow', rowEditing).datagrid('editCell', { index: rowEditing, field: fieldEditing });
        }
        function keyDown(event) {
            if (!editing) {
                return;
            }
            var e = event || window.event;
            var keyCode = e.keyCode || e.which;
            switch (keyCode) {
                case 37:
                    hMove(-1);
                    break;
                case 39:
                    hMove(1);
                    break;
            }
        }
        function hMove(direction) {
            if (rowEditing == -1) {
                return;
            }
            if (fieldEditing == -1) {
                fieldEditing = columnsList[0];
            }
            var ed = $('#customerHistoryList').datagrid('getEditor', { index: rowEditing, field: fieldEditing });
            if (!ed) {
                return;
            }
            if (direction >= 0) {
                direction = 1;
            }
            else {
                direction = -1;
            }
            var tag = null;
            if ($(ed.target).data('textbox')) {
                tag = $(ed.target).textbox('textbox')[0];
            }
            var valueLenth = tag.value.length;
            if (tag == null || tag.readOnly || (tag.selectionStart == 0 || tag.selectionStart == undefined) && direction < 0
                || (tag.selectionEnd == valueLenth || tag.selectionEnd == undefined) && direction >= 0) {
                for (var i = 0; i < columnsList.length; i++) {
                    if (fieldEditing == columnsList[i] && (i + direction) >= 0 && (i + direction) < columnsList.length) {
                        fieldEditing = columnsList[(i + direction)];
                        break;
                    }
                }
                setCellEditable();
            }
        }
        function setEditingLocation(index, field) {
            if (!editing) {
                return;
            }
            var editor = $('#customerHistoryList').datagrid('getEditor', { index: index, field: field });
            if (editor != null) {
                rowEditing = index;
                fieldEditing = field;
            }
        }
    </script>
</head>
<body onload="hideMask()">
    <script>showMask('正在载入数据');</script>
    <div id="toolbar" class="easyui-panel" style="padding:1px;">
        <a href="#" id="detailAdd" class="easyui-linkbutton" data-options="onClick:addItem,iconCls:'icon-add',plain:true">创建</a>
        <a href="#" id="detailDel" class="easyui-linkbutton" data-options="onClick:deleteRecord,iconCls:'icon-remove',plain:true,disabled:true">删除</a>
        <a href="#" id="detailSave" class="easyui-linkbutton" data-options="onClick:submitChange,iconCls:'icon-save',plain:true,disabled:true">保存</a>
    </div>
    <div style="margin:2px"></div>
    <div id="editZone" class="easyui-panel" style="padding:10px;">
        <table>
            <tr>
                <td><input class="easyui-textbox" id="Code" style="width:300px" data-options="label:'客户编号',editable:false"></td>
                <td>&nbsp;</td>
                <td><input class="easyui-textbox" id="Name" style="width:300px" data-options="label:'客户名称:',editable:false"></td>
                <td>&nbsp;</td>
                <td><input class="easyui-textbox" id="Source" style="width:300px" data-options="label:'来源',editable:false"></td>

            </tr>
        </table>
    </div>
    <div style="margin:2px"></div>
    <table id="customerHistoryList" class="easyui-datagrid" data-options="singleSelect:true,method:'get',onClickRow:rowSelected,pagination:true,rownumbers:true,onClickCell:setEditingLocation">
        <thead>
            <tr>
                <th data-options="field:'AutoId',hidden:true"></th>
                <th data-options="field:'Flag',hidden:true"></th>
                <th data-options="field:'CustomerId',hidden:true"></th>
                <th data-options="field:'Note',width:600,align:'right',editor:{type:'textbox'}">跟进历史</th>
                <th data-options="field:'SalesId',width:120,align:'right',
                        formatter:salesFormater,
						editor:{
							type:'combogrid',
							options:
									getComboGridInfo(null,'Name',[[
									{field:'AutoId',title:'ID',width:60},
									{field:'Code',title:'代号',width:60},
									{field:'Name',title:'姓名',width:100}]],employeesInSP)
						}">跟进人</th>
                <th data-options="field:'Date',width:100,align:'right',editor:{type:'datebox',options:{formatter:dateFormatter,parser:dateParser}}">日期</th>
            </tr>
        </thead>
    </table>
</body>
</html>